package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.R
import com.youdianstar.app.databinding.LayoutItemDeviceBinding
import com.youdianstar.app.entity.DeviceEntity



/**
 * File: DeviceListAdapter
 * Author: 82149 Create: 2023/8/3 10:42
 * Changes (from 2023/8/3)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class DeviceListAdapter(val mContext: Context, val dataList: List<DeviceEntity>) : RecyclerView.Adapter<DeviceListAdapter.ListHolder>(){

    class ListHolder(val binding : LayoutItemDeviceBinding) : RecyclerView.ViewHolder(binding.root)
    private var languageNow :String = ""
    init {
        languageNow = LanguageConfig.instance.getLanguageCode()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        return ListHolder(LayoutItemDeviceBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
        dataList[position].let { item ->
            holder.binding.tvSlots.text = item.deviceCount.toString()
            holder.binding.tvSerialNo.text = item.cabinetNo ?: ""
            val txImei = String.format(mContext.getString(R.string.Public_form_tx),mContext.getString(R.string.shop_imei))
            holder.binding.tvIMEI.text = "$txImei ${if (StrUtil.isEmpty(item.imei)) "--" else item.imei}"

            //状态: 0-离线 1-在线 2-锁定 3-待激活
            when (item.state) {
                1 -> {
                    holder.binding.tvState.text = mContext.getString(R.string.shop_online)
                    holder.binding.llItemState.setBackgroundResource(R.drawable.bg_state_normal)
                    holder.binding.ovalState.setBackgroundResource(R.drawable.oval_state_normal)
                }
                0 -> {
                    holder.binding.tvState.text = mContext.getString(R.string.shop_offline)
                    holder.binding.llItemState.setBackgroundResource(R.drawable.bg_state_freeze)
                    holder.binding.ovalState.setBackgroundResource(R.drawable.oval_state_frezze)
                }
            }
            //锁定状态
            if (item.customizeState==2){
                holder.binding.llItemState.visibility = View.VISIBLE
                holder.binding.tvState.text = mContext.getString(R.string.shop_locked)
                holder.binding.llItemState.setBackgroundResource(R.drawable.bg_state_locked)
                holder.binding.ovalState.setBackgroundResource(R.drawable.oval_state_locked)
            }

            //空位状态与对应数量
            holder.binding.itemValue1.text = item.availableNum.toString()
            holder.binding.itemValue2.text = item.returnableNum.toString()
            holder.binding.itemValue3.text = item.damagedNum.toString()
            holder.binding.itemValue4.text = item.abnormalCount.toString()
        }
    }
}